<?php

namespace app\models;

use Yii;
//use common\models\User;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['username', 'email'], 'string', 'max' => 225],
            [['auth_key'], 'string', 'max' => 32],
            [['password', 'password_hash', 'password_reset_token'], 'string', 'max' => 255],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'auth_key' => 'Auth Key',
            'password' => 'Password',
            'password_hash' => 'Password Hash',
            'password_reset_token' => 'Password Reset Token',
            'email' => 'Email',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function setAuth( $data ){

        $c_user = new \common\models\User();
        $c_user->setPassword( $data['User']['password'] );
        $c_user->generateAuthKey( );

//        $data['loginForm']['password'] = $c_user->getPassword();
        $data['User']['id'] = time();
        $data['User']['password_hash'] = $c_user->password_hash;
        $data['User']['auth_key'] = $c_user->auth_key;
        $data['User']['updated_at'] = $data['User']['created_at'] = time();

//        print_r( $data );
        return $data;
    }
}
